package com.carrot.newsgallery.utils;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class NewsChildTableNameParser implements TableNameHandler {

    private static ThreadLocal<String> identity = new ThreadLocal<String>();

    public static void setIdentity(String s){
        identity.set(s);
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        log.info("表名解析已启用: " + tableName);
        String newTableName;
        if (tableName.equals("news_child"))
            newTableName = identity.get();
//        else if(tableName.equals("news_favor"))
//            newTableName=tableName + "_" + identity;
        else
            newTableName = tableName;
        log.info("新表名启用为: "+ newTableName);
        return newTableName;
    }
}
